package com.yhc.controller;

import com.yhc.entity.User;
import com.yhc.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

//此控制器只做页面跳转,或动作执行
@Controller
public class MyController {
    //注入service
    @Resource
    UserService userService;

    // 用户注册页
    @RequestMapping("goreg")
    public String register() {
        //跳转到对应的网页，省略文件扩展名
        return "register";
    }

    //执行注册
    @RequestMapping("reg")
    public String reg(HttpServletRequest request) {
//        获取登录名
        String lname = request.getParameter("reg_name").trim();
//        获取昵称
        String nickname = request.getParameter("reg_nk").trim();
        //获取登录密码
        String lpassword = request.getParameter("reg_pass").trim();
//        获取当前时间
        User user = new User(lname, nickname, lpassword, new Date());
        userService.save(user);
        return "register";
    }

    //用户登录页
    @RequestMapping("golog")
    public String log() {//方法名可任意
        Date date = new Date();
        System.out.println(date);
        return "login";
    }

    //执行登录
    @RequestMapping("log")
    public String login(HttpServletRequest request, Map<String,Object> map) {
        //获取用户信息
        String lname = request.getParameter("username").trim();
        String lpassword = request.getParameter("password").trim();
        System.out.println(lname+lpassword);
        boolean flag =false;
                flag=userService.findUserBylnameAndlpassword(lname, lpassword);
        if(flag){
            return "success";
        }else{//返回当前页
            map.put("error","用户名或密码不正确");
            return "login";
        }
    }
    //用户查询
    //用户转帐

}
